define(function(require, exports, module){
	var $        = require("jquery");
	var util     = require('util');
	var pubjs    = require('pubjs');
	var view     = require('@base/view');
	var labels    = require('modules/labels');

	var format   = labels.format;
	var labelsConfig = labels.labels.config;

	var Base = view.container.extend({
		init: function( config ) {
			config = pubjs.conf(config, {
				'class': 'P-messageMonitor',
				'url': 'monitor/noticeInfo',
				'exportUrl': '/api/dsp/export/messageMonitor/noticeInfo?id=',
				'vModel': {
					'title': null,
					'date': null,
					'account': null,
					'hasTable': false
				}
			});
			this.$id = null;
			this.Super('init', arguments);
		},
		afterBuild: function() {
			var el = this.getDOM();
			el.append([
				'<span class="mb10">',
					'<input class="btn btn-default" type="button" value="'+ LANG('返回') +'">',
				'</span>',
				'<div class="mb10 mt20">',
					'<p>{{title}}</p>',
					'<p>{{date}}</p>',
					'<p><span>报警账号：</span><span>{{account}}</span></p>',
					'<p class="mt30">以下活动触发报警条件：</p>',
					'<table class="content table table-bordered"></table>',
					'<p><a v-show="hasTable" class="pt20 download" href="">下载表格</a></p>',
				'</div>',
				'<div></div>'
			].join(''));
			this.uiBind( el.find('.btn'), 'click', this.goToBack );
			this.vm.mount();
		},
		goToBack: function() {
			pubjs.controller.navigate('admin/messageMonitor');
			return false;
		},
		load: function( id ) {
			pubjs.loading.show();
			this.$id = +id;
			if( !id ) {
				return false;
			}
			pubjs.mc.send(
				this.getConfig('url'),
				{'id': id},
				this.onData.bind( this )
			);
			return this;
		},
		onData: function( err, data ) {
			pubjs.loading.hide();
			if(err) {
				pubjs.error(err.message);
				return false;
			}
			if( !data ) {
				pubjs.alert(LANG('无数据'));
			}
			else {
				this.setData( data );
			}
		},
		setData: function( data ) {
			var el = this.getDOM();
			this.vm.$.title = data.Title;
			this.vm.$.date = util.date( "Y-m-d H:i:s",data.CreateTime );
			this.vm.$.account = pubjs.getUserCurrent('Name');
			if(data.Data && data.Data.length){
				this.vm.$.hasTable = true;
				this.renderTable(data);
			}
			el.find('.download').attr('href',this.getConfig().exportUrl + this.$id);
		},
		reset: function() {
			this.vm.reset();
			this.getDOM().find('table').empty();
			return this;
		},
		renderTable: function(data){
			var self = this;
			var el = self.getDOM();
			//获取配置，根据配置指标渲染表格
			var metrics = pubjs.config('smartMonitorMetric')[data.DimType];

			var table = el.find('table');
			var thead = $('<thead/>').appendTo(table);
			var tbody = $('<tbody/>').appendTo(table);
			//添加标题
			var tr = $('<tr/>');
			tr.append('<th>'+LANG('活动')+'</th><th>'+LANG('报警原因')+'</th>');
			util.each(metrics,function(item,index){
				if(item['_id'] !== "group"){
					tr.append('<th>' + item.Name + '</th>');
				}
			});
			thead.append(tr);


			var result = [];
			//格式化返回的数据
			util.each(data.Data,function(item,index){
				if(item){
					var obj = {};
					for(var i in item){
						if(labelsConfig[i]){
							var formatName = labelsConfig[i].format || labelsConfig[i].render;
							obj[i] = format[formatName] ? format[formatName](item[i]): labels.labels[formatName](null,item,{});
						}
						else{
							obj[i] = item[i];
						}
					}
					result.push(obj);
				}
			})
			//添加表格内容
			util.each(result, function(item,index){
				var tr = $('<tr/>');
				tr.append('<td>' + item["name"] + '</td>');
				tr.append('<td>' + item["reason"] + '</td>');

				util.each(metrics, function(value){
					var id = value['_id'];
					if(id && item.hasOwnProperty(id)){
						tr.append('<td>' + item[id] + '</td>');
					}
				});

				tbody.append(tr);
			});
		}
	});

	exports.base = Base;
});